﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace DataSource
{
    public class LoadCsvFromHS: ILoadData
    {
        private string lv2Path;
        private string date;
        private DateTime st;
        private DateTime et;

        public LoadCsvFromHS(string date)
        {
            lv2Path = @"U:\data\level2\HS";
            this.date = date;
            st = DateTime.ParseExact(date + "093000", "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
            et = DateTime.ParseExact(date + "150000", "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
        }

        public List<Level2Struct> LoadLv2(string code)
        {
            List <Level2Struct> lls = new List<Level2Struct>();
            string se = "";
            if(code[0] == '6')
            {
                se = "SH";
            }
            else
            {
                se = "SZ";
            }
            string filePath = String.Format(@"{0}\{1}\{2}{3}.csv", lv2Path,date, se, code);
            if (!File.Exists(filePath))
            {
                return null;
            }
            StreamReader sr = new StreamReader(filePath, Encoding.Default);
            string[] items = null;
            Dictionary<string, int> colmaps = new Dictionary<string, int>();
            string line;
            bool isColumn = true;
            while (!sr.EndOfStream)
            {
                line = sr.ReadLine();
                items = line.Split(',');
                if (isColumn)
                {
                    for(int i = 0; i < items.Length; i++)
                    {
                        colmaps[items[i]] = i;
                    }
                    isColumn = false;
                    continue;
                }
                Level2Struct ls = new Level2Struct();
                ls.price = float.Parse(items[colmaps["price"]]);
                if (ls.price <= 0)  // 如果没有行情就不要载入
                {
                    continue;
                }
                ls.amount = double.Parse(items[colmaps["amout"]]);
                ls.balance = double.Parse(items[colmaps["balance"]]);
                ls.code = items[colmaps["#ticker"]];
                //ls.time = DateTime.ParseExact(date +" "+ items[colmaps["localtime"]], "yyyyMMdd HH:mm:ss fff", System.Globalization.CultureInfo.CurrentCulture);
                ls.time = DateTime.ParseExact(date + " " + items[colmaps["time"]], "yyyyMMdd HH:mm:ss fff", System.Globalization.CultureInfo.CurrentCulture);
                if (!IsTheMarketTime(ls.time))
                {
                    continue;
                }
                for(int i = 1; i < 6; i++)
                {
                    ls.buy_px.Add(float.Parse(items[colmaps["bid_px" + i.ToString()]]));
                    ls.buy_amount.Add(int.Parse(items[colmaps["bid_amt" + i.ToString()]]));
                    ls.sell_px.Add(float.Parse(items[colmaps["ask_px" + i.ToString()]]));
                    ls.sell_amount.Add(int.Parse(items[colmaps["ask_amt" + i.ToString()]]));
                }
                lls.Add(ls);
            }
            return lls;
        }

        public bool IsTheMarketTime(DateTime dt)
        {
            if ((dt >= st) && (dt <= et))
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public List<Level2Struct> LoadMarketLv2(List<string> stkList)
        {
            List<Level2Struct> lv2List = new List<Level2Struct>(1000000);
            foreach(var tk in stkList)
            {
                Console.WriteLine(tk);
                List<Level2Struct> lls = LoadLv2(tk);
                if (lls != null) lv2List.AddRange(lls);
            }
            return lv2List.OrderBy(t => t.time).ToList();
        }

        public List<TickStruct> LoadMarketTick(List<string> stkList)
        {
            return new List<TickStruct>();
        }

        public List<TickStruct> LoadTick(string code)
        {
            return new List<TickStruct>();
        }


    }
}
